package cn.jiedh.app.meiweidk.mine;

import android.Manifest;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.xutils.ex.HttpException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.UI.AlertDialog;
import cn.jiedh.app.meiweidk.Utils.UI.EmptyRecyclerView;
import cn.jiedh.app.meiweidk.Utils.UI.StateLayout;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.MobileUtils;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.base.BaseActivity;
import cn.jiedh.app.meiweidk.baseadapter.BaseAdapter;
import cn.jiedh.app.meiweidk.baseadapter.BaseViewHolder;
import cn.jiedh.app.meiweidk.entity.CooperationBean;
import cn.jiedh.app.meiweidk.network.ResponseFailure;
import cn.jiedh.app.meiweidk.network.ResponseSuccess;
import cn.jiedh.app.meiweidk.response.BaseResponse;
import cn.jiedh.app.meiweidk.runtimepermissions.PermissionsManager;
import cn.jiedh.app.meiweidk.runtimepermissions.PermissionsResultAction;

/**
 * @name：商务合作
 * @author：司马林
 * @phone：18423134135
 * @createTime：2017/10/20
 */

public class CooperationActivity extends BaseActivity {

    @BindView(R.id.cooperation_rv)
    EmptyRecyclerView rv;
    @BindView(R.id.smartrefreshLayout)
    SmartRefreshLayout smartrefreshLayout;
    @BindView(R.id.statelayout)
    StateLayout statelayout;
    @BindView(R.id.bar_img_back)
    ImageView barImgBack;
    @BindView(R.id.bar_tv_title)
    TextView barTvTitle;
    @BindView(R.id.bar_tv_other)
    TextView barTvOther;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.empty_nn_pic)
    ImageView emptyNnPic;
    @BindView(R.id.empty_nn_btn)
    TextView emptyNnBtn;

    private View empytview;
    private BaseAdapter<CooperationBean> adapter;
    private List<CooperationBean> list = new ArrayList<>();

    @Override
    protected int getLayoutView() {
        return R.layout.activity_cooperation;
    }

    @Override
    protected void initView(View view) {
        barTvTitle.setText("商务合作");
        statelayout.setOnNoClick(new StateLayout.NoNetView() {
            @Override
            public void setOnclck(View view) {
                smartrefreshLayout.autoRefresh();
            }
        });
        adapter = new BaseAdapter<CooperationBean>(this, R.layout.cooperation_item, list) {
            @Override
            public void convert(BaseViewHolder holder, CooperationBean cooperationBean, int position) {
                holder.setText(R.id.cooperation_title, cooperationBean.getCooperationType());
                holder.setText(R.id.cooperation_content, cooperationBean.getContent());
                holder.setText(R.id.email_address,   cooperationBean.getEmail());
                holder.setText(R.id.contact_person,  cooperationBean.getName() + "   " + cooperationBean.getPhone());
                holder.setImageUrl(R.id.cooperation_icon, cooperationBean.getImgUrl());
                LinearLayout phone = holder.getView(R.id.call_phone);
                phone.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        getroot(cooperationBean.getPhone());
                    }
                });
            }
        };
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);
        smartrefreshLayout.setEnableAutoLoadmore(true);//开启自动加载功能（非必须）
        smartrefreshLayout.autoRefresh();//进入页面自动加载
        smartrefreshLayout.setOnRefreshListener(refreshlayout -> refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                statelayout.netviewGone();
                getcooper();
                refreshlayout.finishRefresh();
            }
        }, 2000));
    }

    @Override
    protected void initData() {
        empytview = findViewById(R.id.emptyview);
        emptyNnPic.setImageResource(R.mipmap.jqqd);
        emptyNnBtn.setText("暂无商务合作~");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    public void getroot(String phone) {
        PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(this, new String[]{
                Manifest.permission.CALL_PHONE}, new PermissionsResultAction() {
            @Override
            public void onGranted() {
                callphone(phone);
            }

            @Override
            public void onDenied(String permission) {
                showdialog();
            }
        });
    }

    private void callphone(String phone) {
        new AlertDialog(this).builder().setTitle("拨打").setMsg(phone).setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        }).setPositiveButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.Phone(CooperationActivity.this, phone);
            }
        }).show();
    }

    private void showdialog() {
        new AlertDialog(this).builder().setTitle("未开启拨打权限").setMsg("跳转至设置界面").setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        }).setPositiveButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MobileUtils.getAppDetailSettingIntent(CooperationActivity.this);

            }
        }).show();
    }

    /**
     * 获取商务合作
     */
    private void getcooper() {
        request(this, UrlUtils.BUSINESS, new HashMap<String, Object>(), false, new ResponseSuccess<CooperationData>() {
            @Override
            public void onRequestSucceed(BaseResponse<CooperationData> response) {
                list.clear();
                if (response.getCode() == 0) {
                    List<CooperationBean> mlist = response.getData().getDataList();
                    list.addAll(mlist);
                    rv.setEmptyView(empytview);
                    adapter.notifyDataSetChanged();
                }
            }
        }, new ResponseFailure() {
            @Override
            public void onRequestFailure(HttpException error, String msg) {
                statelayout.netviewGone();
                list.clear();
                rv.setEmptyView(empytview);
                adapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick(R.id.bar_img_back)
    public void onViewClicked() {
        finish();
    }
}
